package org.jianfa.mq.seckill.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.jianfa.mq.seckill.entity.CodeMsg;
import org.jianfa.mq.seckill.entity.po.SeckillGoods;
import org.jianfa.mq.seckill.entity.po.Users;
import org.jianfa.mq.seckill.entity.vo.Result;
import org.jianfa.mq.seckill.mapper.SeckillGoodsMapper;
import org.jianfa.mq.seckill.mapper.UsersMapper;
import org.jianfa.mq.seckill.redis.GoodsKeyPrefix;
import org.jianfa.mq.seckill.redis.RedisService;
import org.jianfa.mq.seckill.rocketmq.RocketMqProducer;
import org.jianfa.mq.seckill.rocketmq.SeckillMessage;
import org.jianfa.mq.seckill.service.SeckillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.List;

@RestController
public class SeckillController {

    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private SeckillGoodsMapper seckillGoodsMapper;
    @Autowired
    private RedisService redisService;
    @Autowired
    private RocketMqProducer rocketMqProducer;
    @Autowired
    private SeckillService seckillService;

    @PostConstruct
    public void postConstruct() {
        List<SeckillGoods> seckillGoods = seckillGoodsMapper.selectAll();
        for (SeckillGoods seckillGood : seckillGoods) {
            redisService.set(GoodsKeyPrefix.seckillGoodsStockPrefix, "" + seckillGood.getGoodsId(), seckillGood.getStockCount());
        }
    }

    @PostMapping("/seckill/doSeckill")
    public Result<Integer> doSeckill(@RequestParam("goodsId") long goodsId) {

        /*检查库存*/
        Integer stockCount = redisService.get(GoodsKeyPrefix.seckillGoodsStockPrefix, "" + goodsId, Integer.class);
        if (stockCount <= 0) {
            return Result.error(CodeMsg.SECKILL_OVER);
        }

        /*发送消息*/
        Subject subject = SecurityUtils.getSubject();
        Object principal = subject.getPrincipal();
        String username = (String) principal;
        Users users = usersMapper.selectByUsername(username);
        if (users == null) {
            return Result.error(CodeMsg.REQUEST_ILLEGAL);
        }

        SeckillMessage seckillMessage = new SeckillMessage();
        seckillMessage.setUserId(users.getId());
        seckillMessage.setGoodsId(goodsId);
        boolean setMessage = rocketMqProducer.sendMessage(seckillMessage);
        if (!setMessage) {
            return Result.error(CodeMsg.SERVER_ERROR);
        }

        return Result.success(0);
    }

    @GetMapping("/seckill/findSeckill")
    public Result<Long>  findSeckill(@RequestParam("goodsId") long goodsId) {
        Subject subject = SecurityUtils.getSubject();
        Object principal = subject.getPrincipal();
        String username = (String) principal;
        Users users = usersMapper.selectByUsername(username);
        Integer usersId = users.getId();

        long result = seckillService.getSeckillResult(usersId.longValue(), goodsId);
        return Result.success(result);
    }
}
